package com.sczmgk.hotline.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.sczmgk.hotline.callback.ParseJsonCallback;
import com.sczmgk.hotline.constant.Constant;
import com.sczmgk.hotline.db.impl.IndicatorTitleDaoImpl;
import com.sczmgk.hotline.entity.IndicatorHomeData;
import com.sczmgk.hotline.entity.IndicatorHomeInfo;
import com.sczmgk.hotline.httpUtils.MyHttp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/5.
 */

public class IndicatorUtils {

    private static final String TAG = IndicatorUtils.class.getSimpleName();

    public interface OnLoadDataFinish{
        void onSuccess();
        void onError(String msg);
    }
    public static void getIndicator(Context context ,OnLoadDataFinish listener){
        getArticleIndicator(context,listener);
    }

    public static void getIndicator(Context context){
        getArticleIndicator(context,null);
    }

    /**
     * 获取文章指示器
     */
    private static void getArticleIndicator(final Context context, final OnLoadDataFinish listener) {

        Map<String,Object> maps = new HashMap<>();
        MyHttp.request(context, Constant.BASE_URL + Constant.ARTICLE_INDICATOR_TITLE_URL, maps, MyHttp.POST, false, null, new ParseJsonCallback() {
            @Override
            public void onVerifyIsNull() {

            }

            @Override
            public void onVerifyError() {

            }

            @Override
            public void onVerifySuccess(String dataRaw) {
                try {
                    parseArticleData(context,dataRaw,listener);
                } catch (Exception e){
                    e.printStackTrace();
                    MyLog.e(TAG,"json解析异常");
                }
            }

            @Override
            public void onServerUnKnownError() {

            }

            @Override
            public void onErrorCodeError(String msg) {
                if (listener != null){
                    listener.onError(msg);
                }
            }
        });
    }

    /**
     * 解析文章数据
     */
    private static void parseArticleData(Context context, String dataRaw, OnLoadDataFinish listener) {
        if (com.sczmgk.frame.http.Tools.isEmpty(dataRaw)){
            MyLog.e(TAG,"获取文章指示器列表返回数据为空");
            return;
        }

        Gson gson = new Gson();
        MyLog.d(TAG , "dataRaw  : " + dataRaw);
        IndicatorHomeData indicatorInfo = gson.fromJson(dataRaw, IndicatorHomeData.class);
        List<IndicatorHomeInfo> newList = indicatorInfo.datalist;

        //获取数据库保存的文章指示器列表
        List<IndicatorHomeInfo> oldList = IndicatorTitleDaoImpl.getIntance(context).findAll();

        //将list转换为json数据
        String newJson = getArrayDataJson(newList);
        String oldJson = getArrayDataJson(oldList);

        //md5加密
        String newMd5 = MyHttp.encrypt(newJson);
        String oldMd5 = MyHttp.encrypt(oldJson);

        MyLog.d(TAG,"newMd5   : " + newMd5 + "      oldMd5   : " + oldMd5);
        //比对md5值 , 如果不同就存数据库 , 如果相同就不存
        if (!newMd5.equals(oldMd5)){
            for (int i = 0; i < newList.size(); i++) {
                IndicatorHomeInfo newListInfo = newList.get(i);
                if (IndicatorTitleDaoImpl.isExists(context, newListInfo.articleid)) {
                    //如果id存在,则更新
                    IndicatorHomeInfo oldListInfo = IndicatorTitleDaoImpl.getIntance(context).findByQuery(newListInfo.articleid);
                    newListInfo.setId(oldListInfo.getId());
                    IndicatorTitleDaoImpl.getIntance(context).updata(newListInfo);
                } else {
                    //如果id不存在,则插入
                    IndicatorTitleDaoImpl.getIntance(context).insert(newListInfo);
                }
            }
        }
        if (listener != null){
            listener.onSuccess();
        }
    }

    /**
     * 将lists生成 JSON 数组
     */
    private static String getArrayDataJson(List<IndicatorHomeInfo> lists) {
        Gson gson = new Gson();
        return gson.toJsonTree(lists).toString();
    }

}
